<?php

/**
 * @name eolinker ams open source，eolinker开源版本
 * @link https://www.eolinker.com/
 * @package eolinker
 * @author www.eolinker.com 广州银云信息科技有限公司 2015-2017
 * eoLinker是目前全球领先、国内最大的在线API接口管理平台，提供自动生成API文档、API自动化测试、Mock测试、团队协作等功能，旨在解决由于前后端分离导致的开发效率低下问题。
 * 如在使用的过程中有任何问题，欢迎加入用户讨论群进行反馈，我们将会以最快的速度，最好的服务态度为您解决问题。
 *
 * eoLinker AMS开源版的开源协议遵循Apache License 2.0，如需获取最新的eolinker开源版以及相关资讯，请访问:https://www.eolinker.com/#/os/download
 *
 * 官方网站：https://www.eolinker.com/
 * 官方博客以及社区：http://blog.eolinker.com/
 * 使用教程以及帮助：http://help.eolinker.com/
 * 商务合作邮箱：market@eolinker.com
 * 用户讨论QQ群：284421832
 */
class GuestController
{
    //返回json类型
    private $returnJson = array('type' => 'guest');

    /**
     * 登录
     */
    public function login()
    {
        $loginName = securelyInput('loginName');
        $loginPassword = securelyInput('loginPassword');
        $server = new GuestModule;
        if (preg_match('/^[0-9a-zA-Z]{32}$/', $loginPassword)) {
            if (preg_match('/^[0-9a-zA-Z][0-9a-zA-Z_]{3,63}$/', $loginName)) {
                $result = $server->login($loginName, $loginPassword);

                if ($result) {
                    $this->returnJson['statusCode'] = '000000';
                    $this->returnJson['userID'] = $_SESSION['userID'];
                } else
                    $this->returnJson['statusCode'] = '120004';
            } else {
                $this->returnJson['statusCode'] = '120001';
                exitOutput(json_encode($this->returnJson));
            }
        } else {
            //密码非法
            $this->returnJson['statusCode'] = '120002';
        }
        exitOutput($this->returnJson);
    }

    /**
     * 用户名注册
     */
    public function register()
    {
        if (!ALLOW_REGISTER) {
            //不允许新用户注册
            $this->returnJson['statusCode'] = '130005';
        } else {
            $userName = securelyInput('userName');
            $loginPassword = securelyInput('userPassword');
            $nickNameLen = mb_strlen(quickInput('userNickName'), 'utf8');
            $userNickName = securelyInput('userNickName');

            //验证用户名,4~16位非纯数字，英文数字下划线组合，只能以英文开头
            if (!preg_match('/^[0-9a-zA-Z][0-9a-zA-Z_]{3,63}$/', $userName)) {
                //用户名非法
                $this->returnJson['statusCode'] = '130001';
            } elseif (!preg_match('/^[0-9a-zA-Z]{32}$/', $loginPassword)) {
                //密码非法
                $this->returnJson['statusCode'] = '130002';
            } elseif (!($nickNameLen == 0 || $nickNameLen <= 16)) {
                //用户名非法
                $this->returnJson['statusCode'] = '130014';
            } else {
                $server = new GuestModule;
                $result = $server->register($userName, $loginPassword, $userNickName);

                if ($result)
                    //注册成功
                    $this->returnJson['statusCode'] = '000000';
                else
                    //注册失败
                    $this->returnJson['statusCode'] = '130005';
            }
        }

        exitOutput($this->returnJson);
    }

    /**
     * 用户查重
     */
    public function checkUserNameExist()
    {
        $userName = securelyInput('userName');
        $server = new GuestModule;
        if (preg_match('/^[0-9a-zA-Z][0-9a-zA-Z_]{3,63}$/', $userName)) {
            $result = $server->checkUserNameExist($userName);
            if ($result)
                //用户名可注册
                $this->returnJson['statusCode'] = '000000';
            else
                //用户名重复
                $this->returnJson['statusCode'] = '130005';
        } else
            //userName格式非法
            $this->returnJson['statusCode'] = '130001';

        exitOutput($this->returnJson);
    }

    /**
     * 检查登录状态
     */
    public function checkLogin()
    {
        $server = new GuestModule;
        $result = $server->checkLogin();

        if ($result) {
            //已登录
            $this->returnJson['statusCode'] = '000000';
        } else {
            //未登录
            $this->returnJson['statusCode'] = '120005';
        }
        exitOutput($this->returnJson);
    }

}

?>